package com.xsz.controller;

import com.xsz.entity.Info;
import com.xsz.entity.User;
import com.xsz.service.InfoService;
import com.xsz.service.UserService;
import com.xsz.util.MD5Utils;
import com.xsz.util.ResultVOUtil;
import com.xsz.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import sun.misc.BASE64Encoder;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.*;

@RestController
@RequestMapping("info")
@Api(value = "产品信息控制类")
public class InfoController {
    @Resource
    InfoService infoService;

    @Value("${fileUpLoadPath}")
    String filePath;

    /**添加产品信息**/
    @ApiOperation(value = "添加产品信息API")
    @PostMapping("addInfo")
    public ResultVO addInfo(HttpServletRequest req, MultipartHttpServletRequest multiReq) throws IOException {
        Info info = new Info();
        try {
            MultipartFile multipartFile = multiReq.getFile("file");
            String filename = multipartFile.getOriginalFilename();
            System.out.println("文件名字：" + multipartFile.getOriginalFilename());
            File file = new File(filePath + filename);
            multipartFile.transferTo(file);
            System.out.println(filePath + filename);
            info.setImage(filename);
        }catch (IOException e){
        }

        info.setTile(req.getParameter("titleinput"));
        info.setContent(req.getParameter("contentinput"));
        info.setProducttype(req.getParameter("producttypeinput"));
        info.setMobile(req.getParameter("mobileinput"));
        info.setWechat(req.getParameter("wechatinput"));
        info.setQq(req.getParameter("qqinput"));
        info.setSd(req.getParameter("sdinput"));
        info.setPrice(req.getParameter("priceinput"));
        return ResultVOUtil.success(infoService.add(info, (User)req.getSession().getAttribute("loginuser")));
    }

    /**添加推荐产品信息**/
    @ApiOperation(value = "添加推荐产品信息API")
    @PostMapping("addvipInfo")
    public ResultVO addvipInfo(HttpServletRequest req, MultipartHttpServletRequest multiReq) throws IOException {
        Info info = new Info();
        try {
            MultipartFile multipartFile = multiReq.getFile("file");
            String filename = multipartFile.getOriginalFilename();
            System.out.println("文件名字：" + multipartFile.getOriginalFilename());
            File file = new File(filePath + filename);
            multipartFile.transferTo(file);
            System.out.println(filePath + filename);
            info.setImage(filename);
        }catch (IOException e){
        }

        info.setTile(req.getParameter("titleinput"));
        info.setContent(req.getParameter("contentinput"));
        info.setProducttype(req.getParameter("producttypeinput"));
        info.setMobile(req.getParameter("mobileinput"));
        info.setWechat(req.getParameter("wechatinput"));
        info.setQq(req.getParameter("qqinput"));
        info.setSd(req.getParameter("sdinput"));
        info.setPrice(req.getParameter("priceinput"));
        info.setType(1);
        return ResultVOUtil.success(infoService.add(info, (User)req.getSession().getAttribute("loginuser")));
    }

    /**修改产品信息**/
    @ApiOperation(value = "修改产品信息API")
    @PostMapping("updateInfo")
    public ResultVO updateInfo(HttpServletRequest req, MultipartHttpServletRequest multiReq) throws IOException {
        Info info = new Info();

        try{
            MultipartFile multipartFile = multiReq.getFile("file");
            String filename = multipartFile.getOriginalFilename();
            System.out.println("文件名字：" + multipartFile.getOriginalFilename());
            File file = new File(filePath + filename);
            multipartFile.transferTo(file);
            System.out.println(filePath + filename);
            info.setImage(filename);
        }catch (IOException e){
            info.setImage(req.getParameter("updateimage"));
        }
        info.setId(req.getParameter("updatetid"));
        info.setCreatetime(req.getParameter("updatetime"));
        info.setTile(req.getParameter("updatetitle"));
        info.setContent(req.getParameter("updatecontent"));
        info.setProducttype(req.getParameter("updateproducttype"));
        info.setMobile(req.getParameter("updatemobile"));
        info.setWechat(req.getParameter("updatewechat"));
        info.setQq(req.getParameter("updateqq"));
        info.setSd(req.getParameter("updatesd"));
        info.setPrice(req.getParameter("updateprice"));
        return ResultVOUtil.success(infoService.update(info, (User)req.getSession().getAttribute("loginuser")));
    }

    /**查询所有产品信息**/
    @ApiOperation(value = "显示所有产品信息API")
    @GetMapping("showAll")
    public ResultVO showAll(){
        return ResultVOUtil.success(infoService.getAll());
    }

    /**根据登录用户id查询产品信息**/
    @ApiOperation(value = "根据登录用户id查询产品信息API")
    @GetMapping("showById")
    public ResultVO showById(HttpServletRequest request){
        HttpSession session=request.getSession();
        User user =(User) session.getAttribute("loginuser");
        System.out.println(user.getId());
        return ResultVOUtil.success(infoService.selectById(user.getId()));
    }

    /**根据infoid查询产品信息**/
    @ApiOperation(value = "根据infoid查询产品信息API")
    @GetMapping("showByinfoId/{id}")
    public ResultVO showByinfoId(@PathVariable("id") String id){
        return ResultVOUtil.success(infoService.selectByInfoId(id));
    }

    /**删除**/
    @ApiOperation(value = "删除API")
    @PostMapping("delete/{id}")
    public ResultVO delete(@PathVariable("id") String id){
        infoService.delete(id);
        return ResultVOUtil.success();
    }

    /**根据产品信息类型查询**/
    @ApiOperation(value = "据产品信息类型查询API")
    @GetMapping("showByType")
    public ResultVO showByType(){
        return ResultVOUtil.success(infoService.selectByType());
    }

    /**根据产品标题和类型查询**/
    @ApiOperation(value = "根据产品标题和类型查询API")
    @GetMapping("showByTitleAndType/{title}/{producttype}")
    public ResultVO showByTitleAndType(@PathVariable("title") String title,@PathVariable("producttype") String producttype){
        return ResultVOUtil.success(infoService.selectByTitleAndProducttype(title,producttype));
    }


    //获得图片的base64码

    public String getImageBase(String src) {
        if(src==null||src==""){
            return "";
        }
        File file = new File(src);
        if(!file.exists()) {
            return "";
        }
        InputStream in = null;
        byte[] data = null;
        try {
            in = new FileInputStream(file);
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }
        try {
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);
    }
}
